
********************************************************************************
********************************************************************************
*************************** THIS IS DO FILE 7 (MCO/FFS) ************************
************* THIS FILE MERGES THE MODEL ESTIMATES WITH THE VISIT DATA *********
********************************************************************************
********************************************************************************

*Version: 2023 March 29


local date "$date"
cap mkdir `"$output/ffs_mco_separation/temp"'
cap mkdir `"$output/ffs_mco_separation/log/7_compute_CIP"'
cap mkdir `"$output/ffs_mco_separation/data"'
cap mkdir `"$output/ffs_mco_separation/model_estimation"'


cap log close 
log using `"$output/ffs_mco_separation/log/7_compute_CIP/7_compute_CIP.smcl"', replace 

di "This run uses code version from 2023/03/29"

use  `"$output/ffs_mco_separation/temp/estimation_data_bin.dta"', clear

gen visit_code_cat_n = 1 if visit_code_cat == "administrative"
replace visit_code_cat_n = 2 if visit_code_cat == "contractual"
replace visit_code_cat_n = 3 if visit_code_cat == "coverage"
replace visit_code_cat_n = 4 if visit_code_cat == "duplicate"
replace visit_code_cat_n = 5 if visit_code_cat == "information"



** continuation value terms **
gen continuation = .99*V_S+.99*0.5772156649
replace continuation = 0 if n_S ==.



//merge in model estimates 
merge m:1 visit_code_cat state pay_type_ffs_mco size_indicator using `"$output/ffs_mco_separation/model_estimation/estimations_bin.dta"' , keepusing(claim_cost claim_cost_se line_cost) nogen 
replace claim_cost = . if claim_cost_se == .

gen extra_line = (-n_S+1)
replace extra_line = 0 if n_S == .
replace claim_cost = 0 if n_S == .

gen indirect_cost = exp(claim_cost +(extra_line*line_cost)+continuation)
gcollapse (sum) indirect_cost*, by(visit_billing_id)
replace indirect_cost = ln(indirect_cost) + 0.5772156649

save `"$output/ffs_mco_separation/temp/cont_value_model_merged.dta"', replace 


//merge into visit level data 
use `"$output/data/$line_file"', clear 

******************************************************************
**************************** Make Pay Type Var *******************
******************************************************************

gen pay_type_ffs_mco = pay_type
replace pay_type_ffs_mco = 1 if pay_type_ffs_mco == 4 // Medicare is one payer (MA and FFS)
replace pay_type_ffs_mco = 4 if pay_type_ffs_mco == 5 // Commercial is pay_type_ffs_mco == 4
lab var pay_type_ffs_mco "1 Medicare; 2 Medicaid FFS; 3 Medicaid MCO; 4 Commercial"

gen pay_type_ffs_mco_st = "Medicare"
replace pay_type_ffs_mco_st = "Medicaid FFS" if pay_type_ffs_mco == 2
replace pay_type_ffs_mco_st = "Medicaid MCO" if pay_type_ffs_mco == 3
replace pay_type_ffs_mco_st = "Commercial" if pay_type_ffs_mco == 4

gen visit_code_cat_n = 1 if visit_code_cat == "administrative"
replace visit_code_cat_n = 2 if visit_code_cat == "contractual"
replace visit_code_cat_n = 3 if visit_code_cat == "coverage"
replace visit_code_cat_n = 4 if visit_code_cat == "duplicate"
replace visit_code_cat_n = 5 if visit_code_cat == "information"

// generate denial indicator 
egen final_paid = rowtotal(Y_?)
gen initial_denial = 1-Y_0
gen final_denial = 1-final_paid
bys visit_billing_id: gegen visit_initial_den = max(initial_denial)
bys visit_billing_id: gegen visit_final_den = max(final_denial)

// generate pi
bys visit_billing_id: gegen pi = sum(line_item_value)
assert pi >=0

// generate final denied amt 
egen Y_max = rowmax(Y_?)
assert Y_max == 0 | Y_max == 1
bys visit_billing_id: gegen denied_amt_tmp = sum(line_item_value) if Y_max ==0
bys visit_billing_id: gegen denied_amt_final = min(denied_amt_tmp)
drop denied_amt_tmp
replace denied_amt_final = 0 if denied_amt_final == . 


// generate initial denied amt 
bys visit_billing_id: gegen denied_visit = min(Y_0)
replace denied_visit = 1-denied_visit

bys visit_billing_id: gegen denied_amt_tmp = sum(line_item_value) if Y_0 ==0
bys visit_billing_id: gegen denied_amt = min(denied_amt_tmp)
drop denied_amt_tmp
replace denied_amt = 0 if denied_amt == . 

// generate RVU proxy of visit 
bys visit_billing_id: gegen RVU = sum(meanallowedMFFS) 

forvalues i = 1/5 {
	
	decode reason_`i', gen(reason_`i'_string)
	gen fineline_`i' = inlist(reason_`i'_string,"","45","1","2","3","253","223","23")
}
cap drop goodobs
egen allfine = rsum(fineline_?)
gen goodobs=allfine==5

bys visit_billing_id : gegen resubmitted = max(R_1)

// switch to visit level 
bys visit_billing_id: keep if _n == 1 
merge 1:1 visit_billing_id using `"$output/ffs_mco_separation/temp/cont_value_model_merged.dta"'
replace indirect_cost = 0 if _merge == 1
drop _merge 

gen CIP = denied_amt - indirect_cost

*****************************************************
*****************************************************
**** DROP NEGATIVE CIP ***
*****************************************************
*****************************************************
sum CIP, d 

drop if CIP < 0 | CIP>r(p99)  //dropping outlier


*****************************************
***** Generate Regression Variables *****
*****************************************
gen ln_pi = ln(pi)	// define allowed amount for visit j as pi

// generate tau and lost revenue 
gen tau = CIP/pi
gen tau_rf = (denied_amt_final/pi)
rename denied_amt_final lost_revenue
rename denied_amt initial_denied_amt

lab var lost_revenue "Final denied amt for a visit"
lab var tau_rf "Tau calculation for each visit if zero resubmission cost"
lab var tau "Tau calculation for each visit"
lab var initial_denied_amt "Amt that is initially denied"
lab var visit_initial_den "At least one line item initially denied"
lab var visit_final_den "At least one line item finally denied"
lab var CIP "CIP at visit level"

save `"$output/ffs_mco_separation/data/regression_data.dta"', replace 
erase `"$output/ffs_mco_separation/temp/cont_value_model_merged.dta"'

log close 
